package com.example.spirngtest.handler;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

/**
 * @program: cet-practice
 * @description:
 * @author: 陈恩涛
 * @create: 2023-08-31 20:48
 **/

@MappedTypes(UUID.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class StringUUIDTypeHandler implements TypeHandler<UUID> {

    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, UUID uuid, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, uuid.toString());
    }

    @Override
    public UUID getResult(ResultSet resultSet, String s) throws SQLException {
        return UUID.fromString(resultSet.getString(s));
    }

    @Override
    public UUID getResult(ResultSet resultSet, int i) throws SQLException {
        return UUID.fromString(resultSet.getString(i));
    }

    @Override
    public UUID getResult(CallableStatement callableStatement, int i) throws SQLException {
        return UUID.fromString(callableStatement.getString(i));
    }
}
